package com.framework.bdf4j.msg.sender;

import java.util.Date;
import java.util.List;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.framework.bdf4j.comm.SpringContextHolder;
import com.framework.bdf4j.msg.entity.domain.SysMsgResult;
import com.framework.bdf4j.msg.entity.dto.SysMessagesSaveDTO;
import com.framework.bdf4j.msg.entity.vo.SysMsgResultVO;
import com.framework.bdf4j.msg.entity.vo.SysUserVO;
import com.framework.bdf4j.msg.service.SysMsgResultService;
import com.framework.bdf4j.msg.util.DtoEntityUtil;

/**
 * 消息发送模板类
 *
 * @author shan.deng
 * @date 2020-07-15 16:06:44
 */
@Component
public abstract class Sender {

    /**
     * 实现这个需要
     * 
     * @param msgId
     *            消息Id
     * @param sysMessagesSaveDTO
     *            消息内容
     * @return
     * @throws RuntimeException
     */
    @Async
    public abstract void send(Integer msgId, SysMessagesSaveDTO sysMessagesSaveDTO, List<SysUserVO> users);

    /**
     * 配置记录对象
     * 
     * @param msgId
     *            消息Id
     * @param results
     *            消息结果内容
     * @return
     */
    @Transactional
    public void recordResult(Integer msgId, List<SysMsgResultVO> results) {
        // 获取记录器
        SysMsgResultService resultService = SpringContextHolder.getBean(SysMsgResultService.class);
        List<SysMsgResult> resultEntities = DtoEntityUtil.trans(results, SysMsgResult.class);
        resultEntities.stream().forEach(resultEntity -> {
            resultEntity.setMsgId(msgId);
            resultEntity.setCreateTime(new Date());
            resultEntity.setUpdateTime(new Date());
            resultService.save(resultEntity);
        });
//        resultService.saveBatch(resultEntities);
    }
}
